// pages/order/location/location.js

const app = getApp();

let City = require('../../../public/json/city.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showCityName:'全国',
    province: [],
    city: [],
    showProvinceId: String,
    title:String,
    returnName:String
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight - app.globalData.navHeight
        });
      }
    });
    this.setData({
      title:options.title,
      showCityId: '',
      province: City,
      city: City
    })
  },

  changeProvince: function (e) {
    var that = this;
    var id = (e.currentTarget.dataset.provinceid) - 2;
   // console.log(id)
   // console.log(City[id]);
    that.setData({
      city: City,
      showCityName: '全国',
      showCityId:''
    })
  },
  changeCity: function (e) {
    var that = this;
    var id = (e.currentTarget.dataset.cityid) - 2;
    var cityData = City[id];
    if (cityData !== undefined) {
      that.setData({
        showCityName: City[id]['name'],
        city: City[id]['child'],
        showCityId: e.currentTarget.dataset.cityid,
      })
    } else {
     // console.log(e.currentTarget.dataset.cityname);
      that.setData({
        returnProvince: e.currentTarget.dataset.province,
        returnCity: e.currentTarget.dataset.cityname,
        showCityId: e.currentTarget.dataset.cityid,
      })
    }
  },
  returnLocation:function(e){

    if (!e.currentTarget.dataset.city) {
     // console.log(e.currentTarget.dataset.city);
      wx.showToast({
        title: '请选择城市',
        icon: 'none'
      })
      return;

    }
   // console.log(e.currentTarget.dataset.title);
    var pages = getCurrentPages();

    var prevPage = pages[pages.length - 2];//当前页面的上一个页面

    if(e.currentTarget.dataset.title=="出发地"){
      prevPage.setData({

        fromCity: e.target.dataset.city,
        fromProvince: e.target.dataset.province,
      })
    } else if (e.currentTarget.dataset.title == "目的地"){

      prevPage.setData({
        toCity: e.target.dataset.city,
        toProvince: e.target.dataset.province,
      })
    }

    wx.navigateBack({

      delta: 1,

    })
  }
})